﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CrankShaftAnalysisSystem.SubForm
{
    public partial class FormInputUuid : Form
    {
        public FormInputUuid()
        {
            InitializeComponent();
        }

        public event Action<string> OnConfirmUuid;
        public bool UuidConfirmed { get; private set; } = false;
        public string DefaultUuid = "RV";

        private void FormPlacePart_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        public void Reset()
        {
            UuidConfirmed = false;
            DefaultUuid = MeasureSystem.pointMeasureConfig.CurrentType;
            txtPartUuid.Text = DefaultUuid;
        }

        private void btnConfirmUuid_Click(object sender, EventArgs e)
        {
            if (txtPartUuid.Text == DefaultUuid)
                return;
            DataStorage.uuid = txtPartUuid.Text;
            if (DataStorage.FileExisting())
            {
                Sunny.UI.UIMessageBox.Show($"结果文件已经存在，请确认零件编号[{DataStorage.uuid}]是否有误.", "ERROR", Sunny.UI.UIStyle.Red);
                if(!uiOverWrite.Active)
                    return;
            }
            UuidConfirmed = true;
            var msg = uiOverWrite.Active ? $"输入零件编号：{txtPartUuid.Text}，已有数据将被覆盖" : $"输入零件编号：{txtPartUuid.Text}";
            var uistyle = uiOverWrite.Active ? Sunny.UI.UIStyle.Orange : Sunny.UI.UIStyle.Blue;
            Sunny.UI.UIMessageBox.Show(msg, "INFO", uistyle);
            if (OnConfirmUuid != null)
                OnConfirmUuid(txtPartUuid.Text);
            uiOverWrite.Active = false;
        }

        private void FormPlacePart_Load(object sender, EventArgs e)
        {
            tmLoop.Start();
            UIHelper.SetPanelToCenter(ref mainPanel);
            ActiveControl = label1;
        }

        private void tmLoop_Tick(object sender, EventArgs e)
        {
            txtPartUuid.Enabled = !UuidConfirmed;
            btnConfirmUuid.Enabled = !UuidConfirmed;
            btnModify.Enabled = UuidConfirmed;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            UuidConfirmed = false;
        }
    }
}
